package com.yiwu.common.domain.entity;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.yiwu.common.enums.ResponseCode;
import com.yiwu.common.exception.BusinessException;
import lombok.*;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * @version 1.0
 * @description TODO
 * @date 2024/5/18 20:32
 */
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@Builder
@TableName(value = "Products")
@Data
public class Products extends Model<Products> {
    /**
     * 商品id
     */
    @TableId
    private Long productId;

    /**
     * 商品分类ID
     */
    @TableField("category_id")
    private Integer categoryId;

    /**
     * 商品的价格
     */
    private BigDecimal price;

    /**
     * 发布商品的用户ID
     */
    @TableField("seller_id")
    private Long sellerId;

    /**
     * 商品图片的URL
     */
    @TableField("image_url")
    private String imageUrl;

    /**
     * 商品的创建时间
     */
    @TableField("created_at")
    private LocalDateTime createdAt;

    /**
     * 商品的最后修改时间
     */
    @TableField("updated_at")
    private LocalDateTime updatedAt;

    /**
     * 商品状态：0-上架，1-删除，2-已售出
     */
    private Integer pstatus;

    /**
     * 商品的详细描述
     */
    private String description;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    // 业务逻辑
    public void markAsDeleted() {
        if (this.pstatus == 1) throw new BusinessException(ResponseCode.DELETE_ERROR);
        this.pstatus = 1; //删除
    }

    public void markAsSold() {
        if (this.pstatus == 1 || this.pstatus == 2) throw new BusinessException(ResponseCode.SOLD_ERROR);
        this.pstatus = 2; //下架
    }

    public void markAsAvailable() {
        if (this.pstatus == 1 || this.pstatus == 0) throw new BusinessException(ResponseCode.AVAILABLE_ERROR);
        this.pstatus = 0; //上架
    }

    public void markStatus(Integer value){
        if(value == 0){
            markAsAvailable();
        }else if(value == 1){
            markAsDeleted();
        }else{
            markAsSold();
        }
    }

    public void isValid() {
        if (price == null || price.compareTo(BigDecimal.ZERO) < 0) {
            throw new BusinessException(ResponseCode.PRICE_ERROR);
        }
        if (description == null || description.trim().isEmpty()) {
            throw new BusinessException(ResponseCode.DESCRIPTION_ERROR);
        }
        if (categoryId == null || categoryId <= 0) {
            throw new BusinessException(ResponseCode.CATEGORY_ERROR);
        }
        if (sellerId == null || sellerId <= 0) {
            throw new BusinessException(ResponseCode.SELLER_ERROR);
        }
    }

    @Override
    public boolean insert() {
        this.productId = IdUtil.getSnowflakeNextId();
        isValid();
        return super.insert();
    }

    @Override
    public boolean updateById() {
        if (this.getProductId() == null) throw new BusinessException(ResponseCode.PARAMETER_ERROR);
        isValid();
        isExistence();
        this.updatedAt = LocalDateTime.now();
        return super.updateById();
    }

    public void isExistence() {
        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", this.productId)
                .eq("seller_id", this.sellerId);
        long count = super.selectCount(queryWrapper);
        if (count <= 0) {
            throw new BusinessException(ResponseCode.PARAMETER_ERROR);
        }
    }
}
